﻿using System.Collections.Generic;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Entity.Sys;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core;
using WebSelfHost.Web.Filters;
using WebSelfHost.Logic;
using WebSelfHost.Utility.Operator;
using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.Permission;
using WebSelfHost.Entity.WebRequestParms; 

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http; 
#endif
namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class PermissionController : BaseController
    {
        private SysPermissionLogic permissionLogic;

        public PermissionController()
        {
            permissionLogic = new SysPermissionLogic();
        }

        /// <summary>
        /// 权限管理主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/permission/index"), AuthorizeChecked]
        public ActionResult Index()
        {
            return Html("/Sys/Permission/Index");
        }

        /// <summary>
        /// 权限管理主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/permission/index"), AuthorizeChecked]
        public ActionResult Index(PermissionIndexParms parms)
        {
            int totalCount = 0;
            var pageData = permissionLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysPermission>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count,
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改权限主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/permission/form"), AuthorizeChecked]
        public ActionResult Form()
        {
            return Html("/Sys/Permission/Form");
        }

        /// <summary>
        /// 新增/修改权限主界面提交信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("system/permission/form"), AuthorizeChecked]
        public ActionResult Form(SysPermission model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = permissionLogic.Insert(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = permissionLogic.Update(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 权限详细信息主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/permission/detail"), AuthorizeChecked]
        public ActionResult Detail()
        {
            return Html("/Sys/Permission/Detail");
        }

        /// <summary>
        /// 权限管理页面删除权限按钮方法
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/permission/delete"), AuthorizeChecked]
        public ActionResult Delete(PrimaryKeyParms parms)
        {
            long count = permissionLogic.GetChildCount(parms.primaryKey);
            if (count == 0)
            {
                int row = permissionLogic.Delete(parms.primaryKey.SplitToList().ToArray());
                return row > 0 ? Success() : Error();
            }
            return Error(string.Format("操作失败，请先删除该项的{0}个子级权限。", count));
        }

        /// <summary>
        /// 通过主键获取权限信息
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/permission/getForm"), LoginChecked]
        public ActionResult GetForm(PrimaryKeyParms parms)
        {
            SysPermission entity = permissionLogic.Get(parms.primaryKey);
            entity.IsEdit = entity.IsEdit == "1" ? "true" : "false";
            entity.IsEnable = entity.IsEnable == "1" ? "true" : "false";
            entity.IsPublic = entity.IsPublic == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 获取父权限
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/permission/getParent"), LoginChecked]
        public ActionResult GetParent()
        {
            var data = permissionLogic.GetList();
            var treeList = new List<TreeSelect>();
            foreach (SysPermission item in data)
            {
                TreeSelect model = new TreeSelect();
                model.id = item.Id;
                model.text = item.Name;
                model.parentId = item.ParentId;
                treeList.Add(model);
            }
            return Content(treeList.ToTreeSelectJson());
        }

        /// <summary>
        /// 获取权限对应图标
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/permission/icon"), LoginChecked]
        public ActionResult Icon()
        {
            return Html("/Sys/Permission/Icon");
        }

        /// <summary>
        /// 权限管理主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/permission/index")]
        public ActionResult AppIndex(PermissionIndexParms parms)
        {
            int totalCount = 0;
            var pageData = permissionLogic.GetList(parms.pageIndex, parms.pageSize, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysPermission>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count,
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改权限数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/permission/form")]
        public ActionResult AppForm(SysPermission model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = permissionLogic.AppInsert(model, model.CreateUser);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = permissionLogic.AppUpdate(model, model.ModifyUser);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/permission/delete")]
        public ActionResult AppDelete(PermissionDeleteParms parms)
        {
            long count = permissionLogic.GetChildCount(parms.primaryKey);
            if (count == 0)
            {
                int row = permissionLogic.Delete(parms.primaryKey.SplitToList().ToArray());
                Logger.OperateInfo($"用户{parms.operateUser}删除了权限");
                return row > 0 ? Success() : Error();
            }
            return Error(string.Format("操作失败，请先删除该项的{0}个子级权限。", count));
        }


        /// <summary>
        /// 根据主键获取权限
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/permission/getForm")]
        public ActionResult AppGetForm(PrimaryKeyParms parms)
        {
            SysPermission entity = permissionLogic.Get(parms.primaryKey);
            entity.IsEdit = entity.IsEdit == "1" ? "true" : "false";
            entity.IsEnable = entity.IsEnable == "1" ? "true" : "false";
            entity.IsPublic = entity.IsPublic == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 获取父权限
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("app/system/permission/getParent")]
        public ActionResult AppGetParent()
        {
            var data = permissionLogic.GetList();
            var treeList = new List<TreeSelect>();
            foreach (SysPermission item in data)
            {
                TreeSelect model = new TreeSelect();
                model.id = item.Id;
                model.text = item.Name;
                model.parentId = item.ParentId;
                treeList.Add(model);
            }
            return Content(treeList.ToTreeSelectJson());
        }
    }

}
